package com.jbp.controller;

import com.jbp.exception.RRException;
import com.jbp.model.Wallet;
import com.jbp.model.WalletExchange;
import com.jbp.model.WalletTransfer;
import com.jbp.service.*;
import com.jbp.service.impl.UserGetScoreRecordServiceImpl;
import com.jbp.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.HashMap;

/**
 * 用户钱包(Wallet)表控制层
 * @author cc
 * @since 2020-06-20 09:43:00
 */
@Api(tags ="APP接口-个人-钱包" )
@ApiIgnore
@RestController
@RequestMapping("/rest/wallet")
public class WalletController {
    @Autowired
    private WalletService walletService;

    @Autowired
    private WalletExchangeService walletExchangeService;

    @Autowired
    private WalletTransferService walletTransferService;

    @Autowired
    private UserGetScoreRecordServiceImpl userGetScoreRecordServiceImpl;
    @Autowired
    private AppConfigService appConfigService;

    @ApiOperation(value="钱包信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Wallet.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @GetMapping("/info")
    public R info(@RequestAttribute("userId")Integer userId){
        return R.ok(walletService.getByUserId(userId) );
    }


    @ApiOperation(value="爱心兑换")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Void.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num",value = "兑换数量", paramType = "body"),
    })
    @PostMapping("/exchange")
    public synchronized R exchange(@RequestAttribute("userId")Integer userId, @RequestBody WalletExchange walletExchange){
        if (walletExchange.getNum()==null) {
            return R.error("参数异常");
        }
        if (walletExchange.getNum().intValue()<1) {
            throw new RRException("参数异常");
        }
        walletExchangeService.exchange(userId, walletExchange);
        return R.ok();
    }

    @ApiOperation(value="红包转账")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = Void.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num",value = "转账金额", paramType = "body"),
            @ApiImplicitParam(name = "toUserId",value = "对方id", paramType = "body"),
    })
    @PostMapping("/transfer")
    public synchronized R transfer(@RequestAttribute("userId")Integer userId, @RequestBody WalletTransfer walletTransfer){
        if (walletTransfer.getNum()==null) {
            return R.error("参数异常");
        }
        if (walletTransfer.getNum().intValue()<1) {
            throw new RRException("参数异常");
        }
        walletTransfer.setNum(BigDecimal.valueOf(walletTransfer.getNum().intValue()));
        if (walletTransfer.getToUserId()==null) {
            return R.error("参数异常");
        }
        //发消息
        if (walletTransfer.getSendMsg()==null || walletTransfer.getSendMsg()!=1) {
            return R.error("参数异常");
        }
        walletTransferService.transfer(userId, walletTransfer);
        return R.ok();
    }

    /**领取积分*/
    @GetMapping("/getScore")
    public R getScore(@RequestAttribute("userId")Integer userId){
        userGetScoreRecordServiceImpl.got(userId);
        return R.ok();
    }

    /**领取积分信息*/
    @GetMapping("/getScoreInfo")
    public R getScoreInfo(@RequestAttribute("userId")Integer userId){
        boolean hasGot = userGetScoreRecordServiceImpl.hasGot(userId);
        int score = appConfigService.getInt("score_get_num");
        int limit = appConfigService.getInt("score_get_limit");
        int on = appConfigService.getInt("score_get_on");
        HashMap<String, Object> map = new HashMap<>();
        map.put("hasGot",hasGot);
        map.put("on",on);
        map.put("limit",limit);
        map.put("score",score);
        return R.ok(map);
    }
}